// Specify the URL where this extension is hosted
var host_url="http://widget.tringme.com/chrome";
var modified=0;

// Let the magic begin - replace the phone numbers with a link
findAndReplace(document.body);

// The workhorse function that finds and replaces all occurences
// of phone number throughout the page.
function findAndReplace(searchNode) {	
	// Simple regular expression to identify phone numbers
	var regex=/[\+\(\)\d][\s\.\-\(\)\d{\d$}]{9,30}/g,
	
	childNodes = (searchNode || document.body).childNodes,
	cnLength = childNodes.length,	
	
	// Exclude the tags when searching
	excludes = 'html,head,style,title,link,meta,script,object,iframe';
	
	while (cnLength--) {
	var currentNode = childNodes[cnLength];

	// If it is a text node and not one of the excluded ones or an anchor tag,
	// then go through it and replace as well
	if ( (currentNode.nodeType === 1 &&
		 (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) || 
		currentNode.nodeName=="A" ) {		
			arguments.callee(currentNode);
	}
	
	var reg_result=regex.test(currentNode.data);		
	if(currentNode.nodeName == "A"){
		if(reg_result)
			reg_result=0;
	}
	
	if (currentNode.nodeType != 3 || !reg_result) {
		continue;
	}
	
	modified=modified+1;		
	if(currentNode.parentNode.nodeName!="A")
	{
		var parent = currentNode.parentNode,
		frag = (function(){
			var html = currentNode.data.replace(regex,r),
			wrap = document.createElement('div'),
			frag = document.createDocumentFragment();
			wrap.innerHTML = html;
			while (wrap.firstChild) {
				frag.appendChild(wrap.firstChild);
			}
			  return frag;	
		})();
		parent.insertBefore(frag, currentNode);
		parent.removeChild(currentNode);
	}
	else { // for anchor tags
		var parent = currentNode.parentNode,	
		frag = (function(){
	
			var html = currentNode.data.replace(regex,'<a href=\"#\" onmouseout="mclosetime()" onclick=\"DisplayMenu(this,\'tr123menu1\',\''+currentNode.parentNode.href+'\',\'$&\');\" >$&</a>'),
			
			wrap = document.createElement('div'),
			frag = document.createDocumentFragment();
			wrap.innerHTML = html;
			while (wrap.firstChild) {
				frag.appendChild(wrap.firstChild);
			}
			return frag;	
		})();
		
		parent.insertBefore(frag, currentNode);
		parent.removeChild(currentNode);
    }
}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function r(str, offset, s){
	strInHREF = trim(str);	
	return '<a href=\"#\" onmouseout="mclosetime()" onclick=\"DisplayMenu(this,\'tr123menu1\',\'\',\''+strInHREF+'\');\">'+ str +'</a>'; 	
}

if(modified >= 1) {
	
	var pop_div = "<div class=\"tr123popup\" id=\"tr123menu1\" onmouseover=\"mcancelclosetime()\" onmouseout=\"mclosetime()\"></div>";
	pop_div += "<div id=\"tr123box1\" style=\"display:none;\"><div style='padding-right:10px;margin-right:45px;height:19px;width:19px;float:right;cursor:pointer' onclick=\"hide_box( 'tr123box1')\" id='close-btn' ></div><div id='login-close' style='cursor:pointer;position:absolute;margin-left:152px;width:18px;height:20px;' onclick=\"hide_box( 'tr123box1')\" ></div><div id=\"tr123content\" style='margin-top:2px;width:160px;padding-top:0px;'><div id=\"tringphone\"></div></div></div>"; 
	
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.src = ''+host_url+'/extension.js';
	InsertHeadNode(newScript);
	
	var swfScript = document.createElement('script');
	swfScript.type = 'text/javascript';
	swfScript.src = ''+host_url+'/swfobject.js';
	InsertHeadNode(swfScript);
	
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = host_url+'/extension.css';
	cssNode.media = 'screen';
	InsertHeadNode(cssNode);
	
	var parent =document.body; 
	firstNode=parent.childNodes[0];
	var wrap = document.createElement('div'),
	frag = document.createDocumentFragment();
	wrap.innerHTML = pop_div;
	while (wrap.firstChild) {
		frag.appendChild(wrap.firstChild);
	}
	parent.insertBefore(frag,firstNode);
	
	PreLoadImages();
}

function PreLoadImages()
{
	var tringme_images=new Array();
	tringme_images[0]=host_url+"/images/phoneicon.png";
	tringme_images[1]=host_url+"/images/smsicon.png";
	tringme_images[2]=host_url+"/images/nomic.png";
	tringme_images[3]=host_url+"/images/buycredits.png";
	tringme_images[4]=host_url+"/images/tringmelogo.png";
	tringme_images[5]=host_url+"/images/bk3.png";
	tringme_images[6]=host_url+"/images/bk4.png";
	tringme_images[7]=host_url+"/images/login.png";
	tringme_images[8]=host_url+"/swf/html_flash.swf";
}


function InsertHeadNode(newscript)
{
	var headID = document.getElementsByTagName("head")[0];   
	fhn=headID.firstElementChild;
	  var wrap = document.createElement('div'),	  
		  frag = document.createDocumentFragment();	  
		  wrap.appendChild(newscript);	  
		  while (wrap.firstChild) {	  
			  frag.appendChild(wrap.firstChild);	  
		  }
	   headID.insertBefore(frag,fhn); 
}



